/*
 * BaseHttpApi.java
 * classes : com.ruint.core.utils.volley.BaseHttpApi
 * @author ruint
 * V 1.0.0
 * Create at 2014-11-25 下午5:42:26
 */
package com.ruint.core.utils.volley;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;

import android.content.Context;

import com.ruint.core.bean.BaseBean;
import com.ruint.core.utils.URLBuilder;
import com.ruint.core.utils.volley.RequestManager.GenericCallback;

/**
 * com.ruint.core.utils.volley.BaseHttpApi
 * 
 * @author ruint <br/>
 *         create at 2014-11-25 下午5:42:26
 */
public abstract class BaseHttpApi extends URLBuilder {

  private static String host;
  private static String service;

  public BaseHttpApi() {
    pre4Api();
  }

  protected abstract String getHost();

  protected abstract String getService();

  private void setHost() {
    BaseHttpApi.host = getHost();
  }

  private void setService() {
    BaseHttpApi.service = getService();
  }

  private RequestManager mRequestManager = RequestManager.getInstance();

  private void pre4Api() {
    setHost();
    setService();
  }

  /**
   * GET request
   * 
   * @param t
   *          A generic type who extents {@link BaseBean} that u want. response
   *          delivered by {@link GenericCallback#onResponse(Object)}
   * @param action
   *          Request method name. Write it in {@link AppConstant#ApiConstant};
   * @param pList
   *          params list
   * @param callback
   *          {@link GenericCallback}
   */
  protected <T> void get(Context context, T t, String action, List<NameValuePair> pList, GenericCallback<T> callback) {
    addCommonParams(pList);
    mRequestManager.get(context, t, makeGetRequest(host, service, action, pList), callback);
  }

  /**
   * POST request with files or images to upload
   * 
   * @param context
   * @param t
   *          A generic type who extents {@link BaseBean} that u want. response
   *          delivered by {@link GenericCallback#onResponse(Object)}
   * @param action
   *          Request method name. Write it in {@link AppConstant#ApiConstant};
   * @param params
   * @param files
   * @param compress
   *          {@link Boolean} Image compress option
   * @param callback
   *          {@link GenericCallback}
   */
  protected <T> void post(Context context, T t, String action, Map<String, String> params, Map<String, File> files,
      boolean compress, GenericCallback<T> callback) {
    addCommonParams(params);
    mRequestManager.post(context, t, makeRequestHeader(host, service, action), params, files, compress, callback);
  }

  /**
   * POST request with files or images to upload
   * 
   * @param context
   * @param t
   *          A generic type who extents {@link BaseBean} that u want. response
   *          delivered by {@link GenericCallback#onResponse(Object)}
   * @param action
   * @param pList
   * @param files
   * @param compress
   *          {@link Boolean} Image compress option
   * @param callback
   *          {@link GenericCallback}
   */
  protected <T> void post(Context context, T t, String action, List<NameValuePair> pList, Map<String, File> files,
      boolean compress, GenericCallback<T> callback) {
    addCommonParams(pList);
    mRequestManager.post(context, t, makeRequestHeader(host, service, action), makeMap(pList), files, compress,
        callback);
  }

  /**
   * POST request without files or images to upload
   * 
   * @param context
   * @param t
   *          A generic type who extents {@link BaseBean} that u want. response
   *          delivered by {@link GenericCallback#onResponse(Object)}
   * @param action
   * @param pList
   * @param callback
   *          {@link GenericCallback}
   */
  protected <T> void post(Context context, T t, String action, List<NameValuePair> pList, GenericCallback<T> callback) {
    addCommonParams(pList);
    mRequestManager.post(context, t, makeRequestHeader(host, service, action), makeMap(pList), null, false, callback);
  }

  /**
   * POST request without files or images to upload
   * 
   * @param context
   * @param t
   *          A generic type who extents {@link BaseBean} that u want. response
   *          delivered by {@link GenericCallback#onResponse(Object)}
   * @param action
   * @param params
   * @param callback
   *          {@link GenericCallback}
   */
  protected <T> void post(Context context, T t, String action, Map<String, String> params, GenericCallback<T> callback) {
    addCommonParams(params);
    mRequestManager.post(context, t, makeRequestHeader(host, service, action), params, null, false, callback);
  }

}
